#!/bin/bash
set -e
set -x
MYDIR="${0%/*}"
cd "$MYDIR/.."
source "recipes/common.sh"

# RECIPE START cross-toolchain
# RECIPE SKIP

stagedir="$xtools/toolchain"
stage1dir="$xtools/stage1"
sysroot="$stagedir/sysroot/$TARGET"
GCC_BRANCH=13

unset CFLAGS
unset CPPFLAGS
unset LDFLAGS
unset CXXFLAGS

rm -rf build/cross-binutils
mkdir -p build/cross-binutils
cd build/cross-binutils

../../binutils/configure \
	--target=$TARGET \
	--host=$BUILD \
	--build=$BUILD \
	--prefix= \
	--disable-bootstap \
	--disable-multiarch \
	--disable-multilib \
	--disable-werror \
	--disable-shared \
	--disable-nls \
	--with-sysroot="$sysroot" \
	--with-build-sysroot="$sysroot" \
	--enable-plugins \
	--with-ld=default \
	--enable-64-bit-bfd \
	--enable-relro \
	--enable-textrel-check=warning \
	$NIL

make -j${JOBS}
make install DESTDIR="$stagedir"
make install DESTDIR="$stage1dir"

cd ../..

rm -rf build/cross-gcc-bootstrap
mkdir -p build/cross-gcc-bootstrap
cd build/cross-gcc-bootstrap

save_PATH="$PATH"
export PATH="$xtools/stage1/bin:$PATH"

../../gcc/configure \
	--target="$TARGET" \
	--host="$BUILD" \
	--build="$BUILD" \
	--prefix= \
	--with-sysroot="$stage1dir/sysroot/$TARGET" \
	--with-build-sysroot="$stage1dir/sysroot/$TARGET" \
	--enable-languages=c \
	--with-newlib \
	--without-headers \
	--disable-nls \
	--disable-shared \
	--disable-threads \
	--disable-libsanitizer \
	--disable-libgomp \
	--disable-libitm \
	--disable-libssp \
	--disable-libvtv \
	--disable-libatomic \
	--disable-libcilkrts \
	--enable-version-specific-runtime-libs \
	--with-gcc-major-version-only \
	--enable-gnu-unique-object \
	--enable-linker-build-id \

make -j${JOBS} all-gcc all-target-libgcc
make -j${JOBS} install-gcc install-target-libgcc DESTDIR="$stage1dir"

cd ../..

# kernel headers 
mkdir -p "$stagedir"
cd kernel-headers/${LINUX_ARCH}
find usr/include | cpio -pumd "$sysroot"
cd ../..


rm -rf build/cross-glibc
mkdir -p build/cross-glibc
cd build/cross-glibc

../../glibc/configure \
	--host="$TARGET" \
	--target="$TARGET" \
	--build="$BUILD" \
	--prefix=$prefix \
	--libdir="$prefix/$target_libdir" \
	--with-sysroot="$sysroot" \
	--with-headers="$sysroot/usr/include" \
	--with-lib="$sysroot/usr/${target_libdir}" \
	--disable-multilib \
	--disable-crypt \
	--disable-werror \
	libc_cv_forced_unwind=yes \
	$NIL

make -j${JOBS}
make install DESTDIR="$sysroot"
# XXX: gcc needs this to locate crt{1,i}.o
mkdir -p "$sysroot/usr/lib"
export PATH="$stagedir/bin:${save_PATH}"

cd ../..
rm -rf build/cross-gcc
mkdir -p build/cross-gcc
cd build/cross-gcc

env \
	ac_cv_file__proc_self_exe=yes \
	gcc_cv_libc_provides_ssp=yes \
../../gcc/configure \
	--target="$TARGET" \
	--host="$BUILD" \
	--build="$BUILD" \
	--prefix= \
	--disable-bootstrap \
	--disable-multiarch \
	--disable-multilib \
	--disable-werror \
	--with-sysroot=$sysroot \
	--with-build-sysroot=$sysroot \
	--enable-version-specific-runtime-libs \
	--with-gcc-major-version-only \
	--enable-languages=c,c++ \
	--disable-nls \
	--disable-libsanitizer \
	--enable-gnu-unique-object \
	--enable-linker-build-id \
	${TARGET_NO_DEFAULT_PIE+--enable-default-pie} \
	$NIL

env \
	ac_cv_file__proc_self_exe=yes \
	gcc_cv_libc_provides_ssp=yes \
make -j ${JOBS}
make install DESTDIR="$stagedir"

if [ -d "$stagedir/lib/gcc/$TARGET/lib64" ]; then
	mv $stagedir/lib/gcc/$TARGET/lib64/libgcc_s.so* $stagedir/lib/gcc/$TARGET/$GCC_BRANCH/
	rmdir $stagedir/lib/gcc/$TARGET/lib64
fi
rm -rf "$sysroot/etc"
rm -rf "$sysroot/var"
rm -rf "$sysroot/sbin"
rm -rf "$sysroot/usr/share"
rm -rf "$sysroot/usr/bin"
rm -rf "$sysroot/usr/sbin"
rm -rf "$sysroot/usr/libexec"
rm -rf "$sysroot/usr/lib64/audit"
rm -rf "$sysroot/usr/lib64/gconv"
rm -rf "$sysroot/share/info"
rm -rf "$sysroot/share/man/man7"

rm -rf build/cross-binutils
rm -rf build/cross-glibc
rm -rf build/cross-gcc
rm -rf build/gcc

# RECIPE END cross-toolchain
